%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Tests for the struct generation module {@link struct} which is what
%%% XML and JSON output is dependent on.
%%%
%%% Uses prog_mock as source material.
%%%
%%% @see struct

-module(struct_tests).

-include_lib("eunit/include/eunit.hrl").

%%%----------------------------------------------------------------------------
%%% Struct tests
%%%----------------------------------------------------------------------------

% Test folder struct is correct, regardless of contents order.
folder_test() ->
  {ok, Folder} = prog_mock:get_record({folder, {0, 20, 1}}),
  Struct = struct:record_to_struct(Folder),
  {folder,[{id,{0,20,1}}], Contents} = Struct,
  ?assertEqual([], lists:subtract(Contents,
     [{folder,[{name,".."},{id,{0,0,1}}]},
      {field,[{name,"YES"},{id,{10,0,1}}]},
      {field,[{name,"NO"},{id,{10,0,2}}]},
      {op,[{name,"Start"},{id,{20,0,1}}]},
      {field,[{name,"n"},{id,{10,0,3}}]},
      {op,[{name,"ShowNotPrime"},{id,{20,0,3}}]},
      {op,[{name,"ShowPrime"},{id,{20,0,2}}]},
      {folder,[{name,"backend"},{id,{0,0,2}}]}])).

% Test field struct.
field_test() ->
  {ok, Field} = prog_mock:get_record({field, {10, 0, 3}}),
  Struct = struct:record_to_struct(Field),
  ?assertEqual({field, [{id, {10, 0, 3}}, {type, integer}]}, Struct).

% Test op struct.
op_test() ->
  {ok, Op} = prog_mock:get_record({op, {20, 0, 5}}),
  Struct = struct:record_to_struct(Op),
  {op, Attributes,
   [{mask, [], []},
    {input, [], Input},
    {outputs, [], Outputs}]} = Struct,

  ?assertEqual([],
               lists:subtract(Attributes,
                              [{id, {20,0,5}},
                               {opType, request_reply},
                               {isComposite, false}])),

  ?assertEqual([],
               lists:subtract(Input,
                              [{field, [{name, "n"}, {id, {10, 0, 3}}]},
                               {field, [{name, "div"}, {id, {10, 0, 4}}]}])),

  ?assertEqual([],
               lists:subtract(Outputs,
                              [{output, [{name, "Yes"}],
                                [{field, [{name, "YES"}, {id, {10, 0, 1}}]}]},
                               {output, [{name, "Maybe"}],
                                [{field, [{name, "MAYBE"}, {id, {10, 0, 5}}]}]},
                               {output, [{name, "No"}],
                                [{field, [{name, "NO"}, {id, {10, 0, 2}}]}]}])).
